<template>
  <div class="container">
    <div class="container-title">
      <p>公告通知</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>社区通知</el-breadcrumb-item>
        <el-breadcrumb-item>公告通知</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search-box">
      <el-form ref="form" :model="searchData">
        <el-form-item>
          <el-select
            @focus="getStatus"
            v-model="searchData.status"
            filterable
            placeholder="状态"
          >
            <el-option
              v-for="(value, name) in statusData"
              :key="name"
              :label="value"
              :value="name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="block">
          <el-date-picker
            v-model="searchData.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-form-item>
          <el-button
            type="success"
            @click="onSearch"
            icon="el-icon-search"
            style="background-color: #0bb197"
            >查询</el-button
          ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="data-list">
      <div class="data-list-title">
        <span>查询结果列表</span>
        <el-button
          type="success"
          style="background-color: #0bb197"
          @click="publishDialog = true"
          >发布公告</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column
          prop="noticeTitle"
          label="标题"
          align="center"
          width="250"
        >
        </el-table-column>
        <el-table-column
          prop="noticeContent"
          label="内容"
          align="center"
          width="600"
        >
        </el-table-column>
        <el-table-column label="发布期限" align="center" width="240">
          <template slot-scope="scope"
            >{{ timeTrans(scope.row.noticeStartTime, 1) }}-{{
              timeTrans(scope.row.noticeEndTime, 1)
            }}</template
          >
        </el-table-column>
        <el-table-column prop="noticeState" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.noticeState == 0">发布中</span>
            <span v-if="scope.row.noticeState == 1">已完成</span>
            <span v-if="scope.row.noticeState == 2">已撤销</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="detail(scope.row)" type="text" size="small"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.noticeState == 0"
              @click="revokeOpen(scope.row)"
              type="text"
              size="small"
              >撤销</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>

    <!-- 发布公告弹窗 -->
    <el-dialog
      title="发布公告"
      :visible.sync="publishDialog"
      width="43%"
      class="publishDialog"
    >
      <el-form :model="publishData">
        <el-form-item label="公告内容">
          <el-input
            type="textarea"
            rows="4"
            placeholder="请输入内容"
            v-model="publishData.noticeContent"
          >
          </el-input>
        </el-form-item>
        <div class="noticeTitle-noticeStartTime">
          <el-form-item label="公告标题">
            <el-input
              v-model="publishData.noticeTitle"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="publishData.noticeStartTime"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <div class="noticeEndTime">
          <el-form-item label="结束日期">
            <el-date-picker
              v-model="publishData.noticeEndTime"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="publishDialog = false">取 消</el-button>
        <el-button type="primary" @click="publishSave">发 布</el-button>
      </div>
    </el-dialog>

    <!-- 详情信息弹窗 -->
    <el-dialog
      title="详细信息"
      :visible.sync="detailDialog"
      width="43%"
      class="detail"
      center
    >
      <div class="detail-information1">
        <p class="noticeTitle">公告标题：{{ detailData.noticeTitle }}</p>
        <p class="noticePublisher">发布人：{{ detailData.noticePublisher }}</p>
      </div>
      <div class="detail-information2">
        <p class="creationTime">发布时间： {{ detailData.creationTime }}</p>
        <p class="releaseDeadline">
          发布期限：{{ detailData.noticeStartTime }}-{{
            detailData.noticeEndTime
          }}
        </p>
      </div>
      <div class="detail-information3">
        公告内容：
        <div class="noticeContent">{{ detailData.noticeContent }}</div>
      </div>
    </el-dialog>

    <!-- 撤销弹窗 -->
    <el-dialog :visible.sync="revokeDialog" width="393px" center class="revoke">
      <span>请问是否确认撤销公告通知？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="revokeDialog = false" class="cancel"
          >取 消</el-button
        >
        <el-button type="primary" @click="revokeSave" class="save"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import time from '../utils/time';
export default {
  mixins: [time],
  data: function () {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      currentPage: 1,
      searchData: { status: '', date: [] },
      //发布公告弹窗
      publishDialog: false,
      publishData: {
        creationTime: '',
        noticeContent: '',
        noticeEndTime: '',
        noticePublisher: '',
        noticeStartTime: '',
        noticeState: 0,
        noticeTitle: '',
      },
      //详情信息弹窗
      row: '',
      detailDialog: false,
      detailData: {},
      //撤销弹窗
      revokeDialog: false,
      revokeData: {},
      //筛选框回显
      statusData: {},
    };
  },
  methods: {
    //渲染
    getData: function () {
      this.$axios({
        url: '/smart/notice/list' + '/' + this.pageNum + '/' + this.pageSize,
        // data: { pageNum: this.pageNum, pageSize: this.pageSize },
      }).then(({ data }) => {
        if ((data.code = '00000')) {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //搜索
    onSearch: function () {
      this.$axios({
        url: '/smart/notice/query',
        method: 'post',
        data: {
          noticeStartTime: this.searchData.date[0],
          noticeEndTime: this.searchData.date[1],
          noticeState: this.searchData.status,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.list.length > 0) {
            this.tableData = data.data.list;
            this.$message.success(data.msg);
          } else {
            this.tableData = data.data.list;
            this.$message.error('暂无数据！');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //重置
    onReset: function () {
      this.searchData.status = '';
      this.searchData.date = [];
      this.getData();
    },
    //详情
    detail: function (row) {
      this.row = row;
      this.detailDialog = true;
      this.$axios({
        url: '/smart/notice/queryById/' + row.id,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.detailData = data.data;
          this.detailData.creationTime = this.timeTrans(
            this.detailData.creationTime,
            0
          );
          this.detailData.noticeStartTime = this.timeTrans(
            this.detailData.noticeStartTime,
            1
          );
          this.detailData.noticeEndTime = this.timeTrans(
            this.detailData.noticeEndTime,
            1
          );
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //撤销弹窗
    revokeOpen: function (row) {
      this.revokeDialog = true;
      this.revokeData = row;
    },
    //撤销保存
    revokeSave: function () {
      this.revokeData.noticeState = 2;
      this.$axios({
        url: '/smart/notice/update',
        method: 'put',
        data: this.revokeData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.$message.success(data.msg);
          this.revokeDialog = false;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //发布公告保存
    publishSave: function () {
      this.publishData.noticePublisher = JSON.parse(
        sessionStorage.getItem('user')
      ).username;
      this.publishData.creationTime = Date.now();
      this.$axios({
        url: '/smart/notice/add',
        method: 'post',
        data: this.publishData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.publishDialog = false;
          this.$message.success(data.msg);
          this.getData();
          this.publishData = {};
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //筛选框状态回显
    getStatus: function () {
      this.$axios({
        url: '/smart/notice/queryByNoticeState',
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.statusData = data.data;
        } else {
          this.$message.error('状态获取失败！');
        }
      });
    },
    //每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    //当前页
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  .container-title {
    width: 98%;
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      color: #333333;
      font-size: 16px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
    .el-breadcrumb-item {
      color: rgba(148, 148, 148, 0.898039215686275);
      font-size: 12px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
  }
  .search-box {
    width: 98%;
    height: 80px;
    background: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #ffffff;
    border-radius: 8px;
    .el-form {
      width: 98%;
      background: #ffffff;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .el-form-item {
        margin-left: 10px;
        margin-bottom: 0;
        .el-select {
          width: 155px;
        }
        .el-button {
          margin: 10px;
        }
        .reset {
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .reset:hover {
          color: #0bb197;
        }
      }
      .block {
        margin-left: 10px;
      }
      .el-date-editor--datetimerange.el-input__inner {
        width: 310px;
      }
    }
  }
  .data-list {
    width: 98%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    margin-top: 20px;
    border-radius: 8px;
    .data-list-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        height: 16px;
        font-family: 'Arial Normal', 'Arial';
        font-weight: 700;
        font-style: normal;
        font-size: 13px;
        color: #3c4145;
      }
    }
    .el-table {
      flex: 1;
      .el-button {
        color: #0bb197;
      }
    }
    .el-pagination {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
}
</style>
<style lang="less">
.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #0bb197;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #0bb197;
  color: #fff;
}

// 发布公告弹窗
.publishDialog .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 95%;
    height: 60px;
    padding: 0;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .el-dialog__title {
      line-height: 60px;
      font-size: 15px;
      font-weight: bold;
      color: #000000;
      margin-left: 10px;
    }
  }
  .el-dialog__body {
    width: 95%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    .el-form {
      width: 98%;
      .el-textarea {
        width: 100%;
      }
    }
    .noticeTitle-noticeStartTime {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .el-form-item {
        width: 48%;
        .el-input {
          width: 100%;
        }
      }
    }
    .noticeEndTime {
      .el-form-item {
        width: 48%;
        .el-input {
          width: 100%;
        }
      }
    }
  }
  .el-dialog__footer {
    width: 95%;
    height: 60px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      height: 60px;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      .el-button--primary {
        width: 66px;
        height: 30px;
        color: #fff;
        background-color: #0bb197;
        border-color: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .el-button--default {
        width: 66px;
        height: 30px;
        color: #333333;
        background-color: #dfe1e8;
        border-color: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .el-button--primary:focus,
      .el-button--primary:hover {
        color: #ffffff;
        border-color: #0bb197;
        background-color: #0bb197;
      }
    }
  }
}
// 详情信息弹窗
.detail .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 100%;
    height: 47px;
    color: #ffffff;
    background: #0bb197;
    padding: 0;
    .el-dialog__title {
      line-height: 47px;
      font-size: 15px;
      color: #ffffff;
      margin-left: 20px;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #ffffff;
    }
  }
  .el-dialog__body {
    width: 95%;
    height: 223px;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    .detail-information1 {
      width: 95%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .noticeTitle {
        width: 60%;
        text-align: left;
      }
      .noticePublisher {
        width: 32%;
        text-align: left;
      }
    }
    .detail-information2 {
      width: 95%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .creationTime {
        width: 60%;
        text-align: left;
      }
      .releaseDeadline {
        width: 32%;
        text-align: left;
      }
    }
    .detail-information3 {
      width: 95%;
      height: 60px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .noticeContent {
        height: 60px;
        line-height: 60px;
      }
    }
  }
}
//撤销弹窗
.revoke .el-dialog {
  .el-dialog__body {
    display: flex;
    justify-content: center;
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
</style>
